package com.sunmnet.j2ee.service.powerdb.designer.impl;

import com.sunmnet.j2ee.core.pub.QueryFilter;
import com.sunmnet.j2ee.core.pub.init.PKInit;
import com.sunmnet.j2ee.core.pub.sqlloader.SqlLoader;
import com.sunmnet.j2ee.dao.BaseDao;
import com.sunmnet.j2ee.entity.powerdb.designer.Database;
import com.sunmnet.j2ee.entity.powerdb.designer.Project;
import com.sunmnet.j2ee.service.impl.BaseServiceImpl;
import com.sunmnet.j2ee.service.powerdb.designer.ProjectService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AccountServiceImpl
 * <p/>
 * 由代码生成器生成
 */
@Repository("/powerdb/designer/projectService")
public class ProjectServiceImpl extends BaseServiceImpl<Project, Long> implements ProjectService {

	@Resource(name = "/powerdb/designer/projectDao")
	@Override
	public void setDao(BaseDao<Project, Long> dao) {
		super.dao = dao;
	}

	public static final Logger LOG = LogManager.getLogger(ProjectServiceImpl.class.getName());

	private static final String CLASS_NAME = ProjectServiceImpl.class.getName();

	private static final String SQL_DELETE_PROJECTACCOUNT = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_DELETE_PROJECTACCOUNT");

	private static final String SQL_INSERT_PROJECTACCOUNT = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_INSERT_PROJECTACCOUNT");

	private static final String SQL_SELECT_PROJECTACCOUNT = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SELECT_PROJECTACCOUNT");

	private static final String SQL_SELECT_REL_PROJECTACCOUNT = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SELECT_REL_PROJECTACCOUNT");

	private static final String SQL_SELECT_PROJECT_BY_ID = SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SELECT_PROJECT_BY_ID");

	private static final String FIND_SQL_FOR_SAVE = "from Project t where upper(t.name)=upper(:name)";

	private static final String FIND_SQL_FOR_UPDATE = "from Project t where upper(t.name)=upper(:name) and t.id!=:id";

	@Override
	public int checkRepeat(Project project) {

		Map params = new HashMap();
		params.put("name", project.getName());

		String sql = FIND_SQL_FOR_SAVE;
		if (project.getId() != null) {
			sql = FIND_SQL_FOR_UPDATE;
			params.put("id", project.getId());
		}

		List list = dao.find(sql, params);
		if (list != null && list.size() > 0) {
			LOG.debug("项目" + project.getName() + "已经存在");
			return 1;
		}
		return 0;
	}

	/**
	 * 删除项目和账户的关系
	 *
	 * @param projectId
	 * @param accountIds
	 * @return
	 */

	public short deleteProjectRelAccount(Long projectId, String accountIds) throws Exception {


		if (accountIds == null) {
			throw new Exception("账户列表为空");
		}

		int result = 0;
		String[] accountIdList = accountIds.split(",");
		Map<String, Object> deleteParams = new HashMap<String, Object>();
		try {

			deleteParams.put("projectId", projectId);
			//deleteParams.put("accountId", accountIds);
			String sql = SQL_DELETE_PROJECTACCOUNT.replace(":accountIds", accountIds);
			result = dao.executeSql(sql, deleteParams);
		} catch (Exception e) {
			throw new Exception("删除失败" + e.getMessage(), e);
		}
		return (short) result;
	}

	/**
	 * 保存项目和账户关系
	 *
	 * @param projectId
	 * @param accountIds
	 * @param right      权限 true 读写 false只读
	 * @return
	 */

	public short saveProjectRelAccount(Long projectId, String accountIds, int right) throws Exception {
		String[] accounts = accountIds.split(",");
		int result = 0;
		try {
			Map<String, Object> map = new HashMap();
			for (int i = 0; i < accounts.length; i++) {
				map.put("id", PKInit.pk.getID());
				map.put("projectId", Long.valueOf(projectId));
				map.put("accountId", Long.valueOf(accounts[i]));
				map.put("projectAuto", right);
				result = dao.executeSql(SQL_INSERT_PROJECTACCOUNT, map);
			}
		} catch (Exception e) {
			throw new Exception("save error, porjectId: " + projectId, e);
		}
		return (short) result;
	}

	public List getAccounts(Long projectId) throws Exception {
		List result = null;
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("projectId", projectId);
			result = dao.findBySql(SQL_SELECT_PROJECTACCOUNT, map);
		} catch (Exception e) {
			throw new Exception("select method error", e);
		}
		return result;
	}

	public List<Map<String, Object>> getProjectInfo(QueryFilter filter) {
		filter.setSql(SQL_SELECT_REL_PROJECTACCOUNT);
		String sql = filter.createSql();
		return dao.findBySql(sql);
	}

	public Map getProjectById(long projectId) throws Exception {

		Map result = null;
		Map<String, Object> map = new HashMap<>();
		map.put("projectId", projectId);

		result = dao.getBySql(SQL_SELECT_PROJECT_BY_ID, map);
		LOG.debug("getProjectById: {} , result: {}", projectId, result);
		return result;
	}

}
